﻿<%@ Page Title="" Language="C#" MasterPageFile="~/Skins/Admin/Views/Shared/Site.Master" Inherits="System.Web.Mvc.ViewPage<OxiteViewModelItem<IEnumerable<Organization>>>" %>
<%@ Import Namespace="Oxite.Extensions" %>
<%@ Import Namespace="ECube.Org.Models" %>
<asp:Content ID="Content4" ContentPlaceHolderID="HeadCssFiles" runat="server">
    <link href='<%= Url.Content(@"~/Scripts/jquery.ui/themes/base/jquery.ui.all.css") %>' rel="stylesheet" type="text/css" />
    <style type="text/css">
        .ui-button{margin: 2px 4px;padding: 2px;text-decoration: none;cursor: pointer;position: relative;text-align: center;}
        .ui-dialog .ui-state-highlight, 
        .ui-dialog .ui-state-error{padding: 4px;}
        .portlet{padding: 10px;}
        .portlet-header{padding: 4px;margin-bottom: 6px;}
    </style>
</asp:Content>
<asp:Content ID="Content5" ContentPlaceHolderID="HeadScripts" runat="server">
<script src="<%= Url.Content("~/Scripts/jquery.form.js") %>" type="text/javascript"></script>
<script src="<%= Url.Content(@"~/Scripts/jquery.ui/ui/jquery-ui-1.8.custom.js") %>" type="text/javascript"></script>
<script src="<%= Url.Content(@"~/Scripts/jsTree/jquery.tree.js") %>" type="text/javascript"></script>
</asp:Content>
<asp:Content ID="Content7" ContentPlaceHolderID="ContentHeader" runat="server">
<h2>组织结构</h2>
</asp:Content>
<asp:Content ID="Content8" ContentPlaceHolderID="MainContent" runat="server">

<div style="margin-right:0.7em;">
<div class="portlet-header ui-widget-header ui-corner-all">组织结构管理</div>
<div class="ui-widget" style="margin:10px 0px;">
    <div class="ui-state-highlight ui-corner-all">
        <p style="margin:0px; padding:3px;"><span class="ui-icon ui-icon-info" style="float: left; margin-right: .3em;"></span>
            <%= Html.Encode(ViewData["Title"]) %>&nbsp;组织结构管理
        </p>
    </div>    
</div>

    <div style="margin:10px 0px;">
        <button id="btn-create-sub" class="ui-button ui-state-default ui-corner-all"> 新建子节点 </button>  
        <button id="btn-delete" class="ui-button ui-state-default ui-corner-all"> 删除 </button>      
    </div>
    
    <div class="ui-widget-content ui-corner-all" style="width:25%;float:left;padding:5px 0px;">
         <div style="margin-left:5px;">组织</div>
         <div id="orgtree"><%= GenOrgTree(Model.Item,null)%></div>
    </div>
    <div style="width:73.5%;float:right;">   
        <div id="tabs">        
        <% Html.RenderPartialFromSkin("Item",Model.Item.FirstOrDefault());%>
        </div>
    </div>

</div>
</asp:Content>
<asp:Content ID="Content9" ContentPlaceHolderID="ScriptVariablesPre" runat="server">
</asp:Content>
<asp:Content ID="Content10" ContentPlaceHolderID="Scripts" runat="server">

<script type="text/javascript">

  function initTab(tabid)
  {
      var li="";
      $(tabid +">div[id^='tabs-']").each(function(){
      li+="<li><a href='#" + $(this).attr("id") + "'>" + $(this).attr("title") + "</a></li>";
      });
      if(li.length > 0)
      {
        $(tabid).prepend("<ul>"+li+"</ul>");
      }
      $(tabid).tabs('destroy'); 
      $(tabid).tabs({selected:$(tabid).attr("selected")});
  }
  $(document).ready(function() {
      initTab("#tabs");
      $("#orgtree").tree({	
            callback:{
                onchange: function(node, tree) {
                     $("#tabs").html("<p><img src='<%= Url.Content("~/Content/images/ajax-loader.gif")%>' width='220' height='19'/></p>");
                     $("#tabs").attr("selected",$("#tabs").tabs('option', 'selected'));
                     $("#tabs").load("<%= Url.Action("Item","Organization")%>/"+$(node).attr("orgid"),function(){initTab("#tabs");});
                     
                }
            }
      });
      
      
      $("#btn-create-sub").click(function() {
         var parentkey = $($.tree.focused().selected[0]).attr("key");
         
         $.post("<%= Url.Action("AddNode","Organization")%>",
             {ParentKey:parentkey,Title:"新节点",Status:"New"},
             function(result){
                $("#tabs").html(result);
                var key=$("#frm-sitenode #Key").val();
                var title=$("#frm-sitenode #Title").val();
                var jqTree =  $.tree.reference("#nodetree");
                var parent = "#" +jqTree.selected[0].id;
                jqTree.create({attributes: { id:"node_"+key,key:key,isNew:"true"}, data:title},parent , -1);
                jqTree.select_branch("#node_"+key);
                initTab("#tabs");
                initForm();
             }
         );
      });
      
      $("#dialog_delete").dialog({
			bgiframe: true,
			resizable: false,
			autoOpen: false,
			minHeight:140,
			modal: true,
			overlay: {backgroundColor: '#000',opacity: 0.5},
			buttons: {
			    '取消': function() {
					$(this).dialog('close');
				},
				'删除': function() {
				    var jqTree =  $.tree.reference("#nodetree");
				    var key = $(jqTree.selected[0]).attr("key");
                    $.post("<%=Url.Action("Remove","SiteNavigator") %>", { key: key },
                            function(data) {
                                $("#dialog_delete").dialog('close'); 
                                if (data == 'true'){
                                  jqTree.remove("#node_" + key);
                                }else{
                                    alert("删除失败！");                                
                                }                         
                                                               
                    });
				}				
			}
		});

      $("#btn-delete").click(function() {
             $("#dialog_delete").dialog('open');
             return false;
      });
 });  
</script>
</asp:Content>
<asp:Content ID="Content11" ContentPlaceHolderID="ScriptVariablesPost" runat="server">
</asp:Content>
<script runat="server">
    private string GenOrgTree(IEnumerable<Organization> orgs,Organization org)
    {
        string sub = "";
        int pid = org == null? 0:org.Id;
        foreach(Organization o in orgs)
        {
            if(o.ParentId == pid)
            {
                sub += GenOrgTree(orgs, o);
            }            
        }
        if (!string.IsNullOrEmpty(sub))
        {
            sub = "<ul>" + sub + "</ul>"; 
        }
        if (org == null)
        {
            return sub; 
        }
        return string.Format("<li id='org_{0}' orgid='{0}'><a href='#'>{1}</a>{2}</li>",org.Id,org.Name,sub);        
    }
</script>
